package view;

/**
 * Created by xulei on 2018/4/4.
 */

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzhoujay.richtext.RichText;

import java.util.List;

import bean.ItemBean;
import jp.wasabeef.richeditor.RichEditor;
import student.andlei.com.student.R;
import utils.StringUtils;

/**
 * 弹窗视图
 */
public class BottomAnswerPopWindow extends PopupWindow implements View.OnClickListener {
    private Activity context;
    private RelativeLayout layout;
    private RichEditor tv_myanswer,
                    tv_rightanswer,
                    tv_resolve,
                    tv_knowledgepoint;
    private String myanswer,rightanswer,reslolve,knowledgepoint;
    public BottomAnswerPopWindow(Activity context, String myanswer,String rightanswer, String reslolve,String knowledgepoint) {
        super(context);
        this.context = context;
        this.myanswer =  myanswer;
        this.rightanswer = rightanswer;
        this.reslolve = reslolve;
        this.knowledgepoint = knowledgepoint;
        initalize();
    }
    public BottomAnswerPopWindow(Activity context, List<ItemBean> list) {
        super(context);
        this.context = context;
        initalize();
    }
    public BottomAnswerPopWindow(Activity context, List<ItemBean> list, TextView editText) {
        super(context);
        this.context = context;
        initalize();
    }

    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.popup_bottom_answer, null);
        layout = view.findViewById(R.id.rlayout_);
        tv_myanswer = view.findViewById(R.id.tv_myanswer);
        tv_rightanswer = view.findViewById(R.id.tv_rightanswer);
        tv_resolve = view.findViewById(R.id.tv_resolve);
        tv_knowledgepoint = view.findViewById(R.id.tv_knowledgepoint);
        if(StringUtils.isNull(myanswer)){ myanswer = ""; }
        if(StringUtils.isNull(rightanswer)){ rightanswer = ""; }
        if(StringUtils.isNull(reslolve)){ reslolve = ""; }
        if(StringUtils.isNull(knowledgepoint)){ knowledgepoint = ""; }
        RichText.initCacheDir(context);
        tv_myanswer.setHtml("我的回答:"+myanswer);
        tv_rightanswer.setHtml("正确答案:"+rightanswer);
        tv_resolve.setHtml("解析:"+reslolve);
        tv_knowledgepoint.setHtml("知识点:"+knowledgepoint);
//        RichText.from("我的回答:"+myanswer).into(tv_myanswer);
//        RichText.from("正确答案:"+rightanswer).into(tv_rightanswer);
//        RichText.from("解析:"+reslolve).into(tv_resolve);
//        RichText.from("知识点:"+knowledgepoint).into(tv_knowledgepoint);
//        tv_myanswer.setText("我的回答:"+myanswer);
//        tv_rightanswer.setText("正确答案:"+rightanswer);
//        tv_resolve.setText("解析:"+reslolve);
//        tv_knowledgepoint.setText("知识点:"+knowledgepoint);

        setContentView(view);
        initWindow();
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view) {
        //弹窗位置设置
        showAsDropDown(view, Math.abs((view.getWidth() - getWidth()) / 2), 10);
        //showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 10, 110);//有偏差
    }

    @Override
    public void onClick(View view) {

    }
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    /**
     * * Checks if the app has permission to write to device storage
     * *
     * * If the app does not has permission then the user will be prompted to
     * * grant permissions
     * *
     * * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}
